(* ::Package:: *)

(************************************************************************)
(* This file was generated automatically by the Mathematica front end.  *)
(* It contains Initialization cells from a Notebook file, which         *)
(* typically will have the same name as this file except ending in      *)
(* ".nb" instead of ".m".                                               *)
(*                                                                      *)
(* This file is intended to be loaded into the Mathematica kernel using *)
(* the package loading commands Get or Needs.  Doing so is equivalent   *)
(* to using the Evaluate Initialization Cells menu command in the front *)
(* end.                                                                 *)
(*                                                                      *)
(* DO NOT EDIT THIS FILE.  This entire file is regenerated              *)
(* automatically each time the parent Notebook file is saved in the     *)
(* Mathematica front end.  Any changes you make to this file will be    *)
(* overwritten.                                                         *)
(************************************************************************)



BeginPackage["W`PCA`"];
J1::usage="J1";
mPCA::usage="";

Begin["`PP`"];


(* ss has dim of m*n*l, m classes and n samples each, each sample is l vector*)
J1[ss_]:=Module[
{Sw,Sb,m,mm},
mm=Map[Mean[#]&,ss];
Sw=Total[Map[Covariance[#]&,ss]];
If[Length[Sw]>0,Sw=Tr[Sw]];
m=Mean[Flatten[ss,1]];

If[Length[m]>0,
Sb=Total[Map[(#-m).(#-m)&,mm]],
Sb=Total[Map[(#-m)*(#-m)&,mm]]];
Sb/Sw];
(* outpou: Eigenvectors, Eigenvalues , pcs*)
mPCA[ss_]:=Module[
{lambda,ev,ppc,psc,tvp,stm},
(* this is how PCA works in math....*)
(*tvp, each row is a vector*)
{psc,tvp}=Chop[KarhunenLoeveDecomposition[ss\[Transpose],"Centered"->True]];
psc=psc\[Transpose];
(*Standardize[ss,Mean,1&].tvp\[Transpose]==psc*)
lambda=Chop[Eigenvalues[Covariance[ss]]];
(* result is also raw-wised*)
{tvp,lambda,psc}
];



End[];
EndPackage[];




